

var isIos
// #ifdef APP-PLUS
isIos = (plus.os.name == "iOS")
// #endif

export const tipMap = {
	'android.permission.READ_EXTERNAL_STORAGE':{
		title:'存储空间/照片权限说明',
		content:'便于您使用该功能上传您的照片/图片/视频及用于更换头像、发送消息、下载、与客服沟通等场景中读取和写入相册和文件内容。'
	},
	'android.permission.WRITE_EXTERNAL_STORAGE':{
		title:'存储空间/照片权限说明',
		content:'便于您使用该功能上传您的照片/图片/视频及用于更换头像、发送消息、下载、与客服沟通等场景中读取和写入相册和文件内容。'
	},
	'android.permission.RECORD_AUDIO':{
		title:'录音权限说明',
		content:'便于您使用该功能录制语音用于聊天发送消息、与客服沟通等场景中。'
	},
	'android.permission.CAMERA':{
		title:'拍照/摄像头权限说明',
		content:'便于您使用该功能拍摄图片/视频及用于更换头像、发送消息、与客服沟通等场景中读取和写入相册和文件内容。'
	}
}



function judgeIosPermissionPush() {
	var result = false;
	var UIApplication = plus.ios.import("UIApplication");
	var app = UIApplication.sharedApplication();
	var enabledTypes = 0;
	if (app.currentUserNotificationSettings) {
		var settings = app.currentUserNotificationSettings();
		enabledTypes = settings.plusGetAttribute("types");
		console.log("enabledTypes1:" + enabledTypes);
		if (enabledTypes == 0) {
			console.log("推送权限没有开启");
		} else {
			result = true;
			console.log("已经开启推送功能!")
		}
		plus.ios.deleteObject(settings);
	} else {
		enabledTypes = app.enabledRemoteNotificationTypes();
		if (enabledTypes == 0) {
			console.log("推送权限没有开启!");
		} else {
			result = true;
			console.log("已经开启推送功能!")
		}
		console.log("enabledTypes2:" + enabledTypes);
	}
	plus.ios.deleteObject(app);
	plus.ios.deleteObject(UIApplication);
	return result;
}


function judgeIosPermissionLocation() {
	var result = false;
	var cllocationManger = plus.ios.import("CLLocationManager");
	var status = cllocationManger.authorizationStatus();
	result = (status != 2)
	console.log("定位权限开启：" + result);
	
	
	plus.ios.deleteObject(cllocationManger);
	return result;
}


function judgeIosPermissionRecord() {
	var result = false;
	var avaudiosession = plus.ios.import("AVAudioSession");
	var avaudio = avaudiosession.sharedInstance();
	var permissionStatus = avaudio.recordPermission();
	console.log("permissionStatus:" + permissionStatus);
	if (permissionStatus == 1684369017 || permissionStatus == 1970168948) {
		console.log("麦克风权限没有开启");
	} else {
		result = true;
		console.log("麦克风权限已经开启");
	}
	plus.ios.deleteObject(avaudiosession);
	return result;
}


function judgeIosPermissionCamera() {
	var result = false;
	var AVCaptureDevice = plus.ios.import("AVCaptureDevice");
	var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide');
	console.log("authStatus:" + authStatus);
	if (authStatus == 3) {
		result = true;
		console.log("相机权限已经开启");
	} else {
		console.log("相机权限没有开启");
	}
	plus.ios.deleteObject(AVCaptureDevice);
	return result;
}


function judgeIosPermissionPhotoLibrary() {
	var result = false;
	var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
	var authStatus = PHPhotoLibrary.authorizationStatus();
	console.log("authStatus:" + authStatus);
	if (authStatus == 3) {
		result = true;
		console.log("相册权限已经开启");
	} else {
		console.log("相册权限没有开启");
	}
	plus.ios.deleteObject(PHPhotoLibrary);
	return result;
}


function judgeIosPermissionContact() {
	var result = false;
	var CNContactStore = plus.ios.import("CNContactStore");
	var cnAuthStatus = CNContactStore.authorizationStatusForEntityType(0);
	if (cnAuthStatus == 3) {
		result = true;
		console.log("通讯录权限已经开启");
	} else {
		console.log("通讯录权限没有开启");
	}
	plus.ios.deleteObject(CNContactStore);
	return result;
}


function judgeIosPermissionCalendar() {
	var result = false;
	var EKEventStore = plus.ios.import("EKEventStore");
	var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(0);
	if (ekAuthStatus == 3) {
		result = true;
		console.log("日历权限已经开启");
	} else {
		console.log("日历权限没有开启");
	}
	plus.ios.deleteObject(EKEventStore);
	return result;
}


function judgeIosPermissionMemo() {
	var result = false;
	var EKEventStore = plus.ios.import("EKEventStore");
	var ekAuthStatus = EKEventStore.authorizationStatusForEntityType(1);
	if (ekAuthStatus == 3) {
		result = true;
		console.log("备忘录权限已经开启");
	} else {
		console.log("备忘录权限没有开启");
	}
	plus.ios.deleteObject(EKEventStore);
	return result;
}

function nativeObjView(tip){
	if(!tip){
		return 
	}
	const systemInfo = uni.getSystemInfoSync();  
	const statusBarHeight = systemInfo.statusBarHeight;  
	const navigationBarHeight = systemInfo.platform === 'android' ? 48 : 44; 
	const totalHeight = statusBarHeight + navigationBarHeight;
	let view = new plus.nativeObj.View('per-modal', {  
		top: '0px',  
		left: '0px',  
		width: '100%',  
		backgroundColor: 'rgba(0,0,0,0.3)',  
		
	})
	view.drawRect({  
		color: '#fff',  
		radius: '5px'  
	}, {  
		top: totalHeight + 'px',  
		left: '5%',  
		width: '90%',  
		height: "100px",  
	})
	view.drawText(tip.title, {  
		top: totalHeight + 5 + 'px',  
		left: "8%",  
		height: "30px"  
	}, {  
		align: "left",  
		color: "#000",  
	}, {  
		onClick: function (e) {  
			console.log(e);  
		}  
	})  
	view.drawText(tip.content, {  
		top: totalHeight + 35 + 'px',  
		height: "60px",  
		left: "8%",  
		width: "84%"  
	}, {  
		whiteSpace: 'normal',  
		size: "14px",  
		align: "left",  
		color: "#656563"  
	})
	function show() {  
		view = plus.nativeObj.View.getViewById('per-modal');  
		view.show()  
		view = null  
	}  

	function close() {  
		view = plus.nativeObj.View.getViewById('per-modal');  
		view.close();  
		view = null  
	}  
	return {  
		show,  
		close  
	} 
}


export function requestAndroidPermission(permissionID) {
	const permVal = uni.getStorageSync(permissionID)
	let viewObj = null
	if(!permVal){
		const tip = tipMap[permissionID]
		viewObj = tip?nativeObjView(tip):null
		viewObj&&viewObj.show&&viewObj.show()
	}
	
	return new Promise((resolve, reject) => {
		plus.android.requestPermissions(
			[permissionID], 
			function(resultObj) {
				var result = 0;
				for (var i = 0; i < resultObj.granted.length; i++) {
					var grantedPermission = resultObj.granted[i];
					console.log('已获取的权限：' + grantedPermission);
					result = 1
				}
				for (var i = 0; i < resultObj.deniedPresent.length; i++) {
					var deniedPresentPermission = resultObj.deniedPresent[i];
					console.log('拒绝本次申请的权限：' + deniedPresentPermission);
					result = 0
				}
				for (var i = 0; i < resultObj.deniedAlways.length; i++) {
					var deniedAlwaysPermission = resultObj.deniedAlways[i];
					console.log('永久拒绝申请的权限：' + deniedAlwaysPermission);
					result = -1
				}
				if (result === 1) {  
					
					uni.setStorageSync(permissionID,true)
				} else if (result === 0) {  
					
					uni.setStorageSync(permissionID,false)
				} else {  
					uni.setStorageSync(permissionID,true)
				}  
				resolve(result);
				viewObj&&viewObj.close()
				
				
				
				
			},
			function(error) {
				console.log('申请权限错误：' + error.code + " = " + error.message);
				resolve({
					code: error.code,
					message: error.message
				});
				viewObj&&viewObj.close()
			}
		);
	});
}


export function judgeIosPermission(permissionID) {
	if (permissionID == "location") {
		return judgeIosPermissionLocation()
	} else if (permissionID == "camera") {
		return judgeIosPermissionCamera()
	} else if (permissionID == "photoLibrary") {
		return judgeIosPermissionPhotoLibrary()
	} else if (permissionID == "record") {
		return judgeIosPermissionRecord()
	} else if (permissionID == "push") {
		return judgeIosPermissionPush()
	} else if (permissionID == "contact") {
		return judgeIosPermissionContact()
	} else if (permissionID == "calendar") {
		return judgeIosPermissionCalendar()
	} else if (permissionID == "memo") {
		return judgeIosPermissionMemo()
	}
	return false;
}


export function gotoAppPermissionSetting() {
	if (isIos) {
		var UIApplication = plus.ios.import("UIApplication");
		var application2 = UIApplication.sharedApplication();
		var NSURL2 = plus.ios.import("NSURL");
		
		var setting2 = NSURL2.URLWithString("app-settings:");
		application2.openURL(setting2);

		plus.ios.deleteObject(setting2);
		plus.ios.deleteObject(NSURL2);
		plus.ios.deleteObject(application2);
	} else {
		
		var Intent = plus.android.importClass("android.content.Intent");
		var Settings = plus.android.importClass("android.provider.Settings");
		var Uri = plus.android.importClass("android.net.Uri");
		var mainActivity = plus.android.runtimeMainActivity();
		var intent = new Intent();
		intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
		var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
		intent.setData(uri);
		mainActivity.startActivity(intent);
	}
}



export function checkSystemEnableLocation() {
	if (isIos) {
		var result = false;
		var cllocationManger = plus.ios.import("CLLocationManager");
		var result = cllocationManger.locationServicesEnabled();
		console.log("系统定位开启:" + result);
		plus.ios.deleteObject(cllocationManger);
		return result;
	} else {
		var context = plus.android.importClass("android.content.Context");
		var locationManager = plus.android.importClass("android.location.LocationManager");
		var main = plus.android.runtimeMainActivity();
		var mainSvr = main.getSystemService(context.LOCATION_SERVICE);
		var result = mainSvr.isProviderEnabled(locationManager.GPS_PROVIDER);
		console.log("系统定位开启:" + result);
		return result
	}
}

export const judgeAndroidPermission=(permission)=>{
	const res = plus.navigator.checkPermission(permission)
	if(res=='authorized'){
		return true
	}else{
		
		return false
	}
}
export default {
	judgeIosPermission: judgeIosPermission,
	judgeAndroidPermission:judgeAndroidPermission,
	requestAndroidPermission: requestAndroidPermission,
	checkSystemEnableLocation: checkSystemEnableLocation,
	gotoAppPermissionSetting: gotoAppPermissionSetting
}
